package ConvertPDF;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXMLFile {

	DocumentBuilderFactory docFactory;
	DocumentBuilder docBuilder;
	//char temp;
	String infoName;
	String infoId;
	String infoAuthors;
	String infoLyrics;
	String infoExtra;
	String infoLyricsExtra;
	int i, flag, flag1;
	
	
	public WriteXMLFile() {
		
		infoName = "";
		infoId = "";
		infoAuthors = "";
		infoLyrics = "";
		infoExtra = "";
		infoLyricsExtra = "";
		i = 0;
		flag = 0;
		
	}
	
	public void CreateXMLFile(String page){
		
		try{
			
			docFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docFactory.newDocumentBuilder();
			
			Document doc = docBuilder.newDocument();
			
			// tao nut goc
			Element rootElement = doc.createElement("listsong");
			doc.appendChild(rootElement);
			
			//TH1
			//name
			//tacgia
			//id
			//loi
			
			//TH2
			//tac gia chua : chua ca
			//ten
			//id
			//loi
			// tac gia nhac tho : 
			
			//TH3
			//ten bai hat
			//loi
			//tac gia
			//id
			//loi them
			
			while(page.charAt(i) !='+'){
				
				
				//nodeName
					if(page.charAt(i)=='\n'){
						
						if(infoName == ""){
							
							//lay cai chuoi chua ten bai hat
							infoName = page.substring(0, i);
							
							if(infoName.contains(":")|| infoName.contains("ca")){
								flag = 1 ;
								infoAuthors = page.substring(0 , i);
								infoName = "";
							}
							
							// cat bo chuoi co ten bai hat ra khoi chuoi
							page = page.substring(i+1);
							//reset lai chi so
							i = 0;
							
						}
						
					}
						
				//node Authors
					if(page.charAt(i)=='\n'){
						
						if(infoAuthors == "" ){
													
							//lay cai chuoi chua ten tac gia
							infoAuthors = page.substring(0, i);
							
							//neu tac gia dai hon 25 ky tu thi do la loi bai hat
							if(infoAuthors.length()>25){
								flag = 1 ;
								infoLyrics = page.substring(0 , i);
								infoAuthors = "";
							}
							// cat bo chuoi co ten bai hat ra khoi chuoi
							page = page.substring(i+1);
							//reset lai chi so mang
							i = 0;
						}
						
					}
						
				//nodeId
					if(page.charAt(i) == '\n'){
						
						if(infoId == ""){
							
							//lay cai chuoi chua ten tac gia
							infoId = page.substring(0, i);
							// cat bo chuoi co ten bai hat ra khoi chuoi
							page = page.substring(i+1);
							//reset lai chi so mang
							i = 0;
							
						}
						
					}
						
				//nodeLyric
					if(page.charAt(i) == '\n'){
						
						if(infoLyrics == ""){
							
							//lay cai chuoi chua ten tac gia
							infoLyrics = page.substring(0, i);
							// cat bo chuoi co ten bai hat ra khoi chuoi
							page = page.substring(i+1);
							//reset lai chi so mang
							i = 0;
							
						}	
					}
					
				//thong tin phu cua tac gia hoac bai hat
					if(page.charAt(i) == '\n' && flag == 1){
						if(infoExtra == ""){
							
							//lay cai chuoi chua ten tac gia phu hay phan du cua bai hat
							infoExtra = page.substring(0, i);
							
							//kiem tra neu phan cong them la loi bai hat tuc la co chua dau ...
							if(infoExtra.contains("...")){
								infoLyrics = infoLyrics + infoExtra;
							}
							else {
								//cong them thong tin vao phan tac gia
								infoAuthors = infoAuthors + " " + infoExtra;
							}
							
							// cat bo chuoi co ten bai hat ra khoi chuoi
							page = page.substring(i+1);
							//reset lai chi so mang
							i = 0;
							//set lai count = 0
							flag = 0;
						}
					}
					
					
						
						
					if(infoName != "" && infoAuthors != "" && infoId != "" && infoLyrics != "" && flag == 0){
						
						//tao song
						Element nodeSong = doc.createElement("song");
						rootElement.appendChild(nodeSong);
						
						// bo noi dung vao node name
						Element nodeName = doc.createElement("name");
						nodeName.appendChild(doc.createTextNode(infoName));
						nodeSong.appendChild(nodeName);
						
						// bo noi dung vao node Authors
						Element nodeAuthor = doc.createElement("author");
						nodeAuthor.appendChild(doc.createTextNode(infoAuthors));
						nodeSong.appendChild(nodeAuthor);
						
						//bo noi dung vao node id
						Element nodeId = doc.createElement("id");
						nodeId.appendChild(doc.createTextNode(infoId));
						nodeSong.appendChild(nodeId);
						
						//bo noi dung vao node lyric
						Element nodeLyric = doc.createElement("lyric");
						nodeLyric.appendChild(doc.createTextNode(infoLyrics));
						nodeSong.appendChild(nodeLyric);
						
						infoName = "";
						infoId = "";
						infoLyrics = "";
						infoAuthors = "";
						infoExtra = "";
						infoLyricsExtra = "";
					}
					i++;
					
					
			}
				// viet noi dung vao xml
				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				StreamResult result = new StreamResult(new File("D:\\file.xml"));
				
				transformer.transform(source, result);
			}	
			catch(ParserConfigurationException pce){
				pce.printStackTrace();
				
			} 
			catch (TransformerConfigurationException ex) {
				ex.printStackTrace();
			}
			catch (TransformerException e) {
					e.printStackTrace();
			}
		
	}
	
	//kiem tra chu thuong = 1 la chu hoa = 0 la chu thuong
	/*private int CheckLowerCase(char c){
		
		if(c >= 'A' && c <= 'Z')
		{
			return 1;
		}
		if(c ==' '){
			
			return 1;
		}
		
		return 0;
	}*/
	
}
